import AMapLoader from '@amap/amap-jsapi-loader';
interface LoadOptions {
	plugins?: string[];
}

/*export async function loadAmap(options: LoadOptions = {}) {
	const AMap = await AMapLoader.load({
		key: '59a4fd8625d744477eb1ade936ef2923',
		version: '2.0',
		plugins: ['AMap.MarkerCluster', 'AMap.Geocoder', 'AMap.LngLat', 'AMap.DistrictSearch', 'AMap.MouseTool', ...(options.plugins || [])],
	});
	return AMap;
}*/
export const loadAmap = (options: LoadOptions = {}) => {
	return new Promise<any>((res, rej) => {
		// if (window.AMap) return res(window.AMap)
		AMapLoader.load({
			key: '59a4fd8625d744477eb1ade936ef2923', // 申请好的Web端开发者Key，首次调用 load 时必填
			version: '2.0', // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15 ！！此版本必须固定，因为代码中大量使用了高德地图实例中的私有属性，他们在不同的版本中，属性位置可能不一样
			plugins: [
				'AMap.PolygonEditor',
				'AMap.PolylineEditor',
				'AMap.DistrictSearch',
				'AMap.MarkerCluster',
				'AMap.MouseTool',
				'AMap.Marker',
				'AMap.RoadInfoSearch',
				'AMap.Geocoder',
				'AMap.PlaceSearch',
				'AMap.TruckDriving',
				'AMap.ToolBar',
				'AMap.ControlBar',
				'AMap.RangingTool',
				'AMap.MoveAnimation',
				'AMap.AutoComplete',
				'AMap.LabelMarker',
				'AMap.Scale',
				'AMap.Weather',
				'AMap.Driving',
				...(options.plugins || []),
			], // 需要使用的的插件列表，如比例尺'AMap.Scale'等
			AMapUI: {
				plugins: ['misc/PoiPicker', 'control/BasicControl', 'misc/PathSimplifier'],
			},
			Loca: {
				// 是否加载 Loca， 缺省不加载
				version: '2.0.0', // Loca 版本，缺省 1.3.2
			},
		})
			.then((AMap) => {
				window.AMap = AMap;
				res(window.AMap);
			})
			.catch((e) => {
				console.log(e);
				rej(e);
			});
	});
};
